package com.invengo.module_check.ui.fragment.parallel;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentParallelBinding;

import java.io.Serializable;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;

import static com.invengo.module_check.data.entity.ActionArgs.ACTION_PARALLEL;
import static com.invengo.module_check.data.entity.BundleKey.KEY_OP_STATE;

public class ParallelFragment extends BaseFragment<FragmentParallelBinding, ParallelViewModel> {

    ShelfBean shelfBeans;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_parallel;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        shelfBeans = (ShelfBean) getArguments().getSerializable(BundleKey.KEY_SHELFS);
//      List<BookBean> bookBeans = (List<BookBean>) getArguments().getSerializable(BundleKey.KEY_BOOKS);
        viewModel.updateShelfBean(shelfBeans);
    }

    private MaterialDialog listingDialog;

    @Override
    public void initViewObservable() {
        viewModel.ui.resultEvent.observe(this, bookBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.KEY_BOOKS, (Serializable) bookBeans);
            bundle.putSerializable(BundleKey.KEY_ACTION, ACTION_PARALLEL);
            bundle.putBoolean(KEY_OP_STATE, bookBeans != null);
            bundle.putString(BundleKey.KEY_CURRENT_SHELF, viewModel.currentShelfName.getValue());
            bundle.putString(BundleKey.KEY_TITLE, "顺架");
            bundle.putSerializable(BundleKey.KEY_INVENTORY_PARALLERS_COUNT, (Serializable) viewModel.getStatistics());
            NavHostFragment.findNavController(this).navigate(ACTION_PARALLEL.getResultAction(), bundle);
        });
        viewModel.ui.listingEvent.observe(this, bookBeans -> {
            if (listingDialog == null)
                listingDialog = MaterialDialogUtils.showBasicDialog(getActivity(), "错架提醒!", "是否将错架图书上架！")
                        .positiveText("完成顺架")
                        .negativeText("上架图书")
                        .onPositive((dialog, which) -> {
                            viewModel.updateParallelLogs();
                        })
                        .onNegative((dialog, which) -> {
                            viewModel.listingErrorBook(bookBeans);
                        }).build();
            if (!listingDialog.isShowing())
                listingDialog.show();
        });

    }
}
